//
//  LocationManager.m
//  HdStore
//
//  Created by zhenqiu on 15/12/21.
//  Copyright © 2015年 zhenqiu. All rights reserved.
//

#import "LocationManager.h"

@interface LocationRequest : NSObject
{
    NSString * requestid ;
    LocationComplete block;
}
@property (nonatomic,strong) LocationComplete block;

@end

@implementation LocationRequest
@synthesize block;

@end


@interface LocationManager()

{
    CLLocationManager * cclocationManager;
    
}
@property (nonatomic,strong) CLLocationManager * locationManager;

-(id) init;

@end

@implementation LocationManager
@synthesize locationManager;
@synthesize locationRequests;
@synthesize isUpdating;
@synthesize requestDic;


+(id)getInstance
{
    static dispatch_once_t once ;
    static id instance = nil;
    dispatch_once(&once, ^{
        
        instance = [[LocationManager alloc] init];
        
    });
    
    return instance;

}


- (id) init
{
    self =[super init];
    if(self)
    {
        // to do
        
        cclocationManager = [[CLLocationManager alloc] init];
        //[cclocationManager setDelegate: self];
        cclocationManager.delegate = self;
        
        cclocationManager.desiredAccuracy = kCLLocationAccuracyBest;
        requestDic = [[NSMutableDictionary alloc] initWithCapacity:10];
        isUpdating = NO;
        
    }
    
    return self;
    
}




-(void)startLocation{
    
    if([CLLocationManager locationServicesEnabled])
    {
        [cclocationManager startUpdatingLocation];
        isUpdating =YES;
        
    }
    else{
        
        GYNLog(@"go to setting");
        
    }
    
    
}

-(void) stopLocaton{
    
    [requestDic removeAllObjects];
    [cclocationManager stopUpdatingLocation];
    isUpdating = NO;
    
}


// location delegate

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    
    if(locations&&[locations count] >0)
    {
        CLLocation * lastLocation = [locations lastObject];
        
        
        for (NSString*  key  in [requestDic allKeys]) {
            
            LocationComplete block = (LocationComplete)[requestDic objectForKey:key];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(lastLocation,nil);
            
            });
            
        }
        
        [self stopLocaton];
    }
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    
    for (NSString*  key  in [requestDic allKeys]) {
        
        
        LocationComplete block = (LocationComplete)[requestDic objectForKey:key];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            block(nil,error);
            
        });
    }
    
    [self stopLocaton];
    
    
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    
    GYNLog(@"status = %d", status);
    
    switch (status) {
            
            
        case kCLAuthorizationStatusNotDetermined:
        
        {
            if ([cclocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
            {
                [cclocationManager requestWhenInUseAuthorization];
            }
           
        }
            
        break;
            
        case kCLAuthorizationStatusAuthorizedAlways:
        {
            
//            if ([cclocationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
//            {
//                [cclocationManager requestAlwaysAuthorization];
//            }

            
        }
            
        break;
            
            
        case kCLAuthorizationStatusAuthorizedWhenInUse:{
            
//            if ([cclocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
//            {
//                [cclocationManager requestWhenInUseAuthorization];
//            }

            
        }
            
        break;
        case kCLAuthorizationStatusDenied:
            
        {
            GYNLog(@"you can't get detail location");
//            NSError * error = [NSError errorWithDomain:kCLErrorDomain code:kCLErrorDenied userInfo:nil];
//            
//            for (NSString*  key  in [requestDic allKeys]) {
//                
//                
//                LocationComplete block = (LocationComplete)[requestDic objectForKey:key];
//                
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    
//                    block(nil,error);
//                    
//                });
//            }
//            [self stopLocaton];
        }
        
        break;
            
        default:
            break;
    }
    
    
}


-(void) getLocation:(void (^)(CLLocation * location , NSError * error)) complete{
    
    
    if (isUpdating) {
        
        [requestDic setValue:complete forKey:[NSString stringWithFormat:@"%lu",(long)[complete hash]]];
        
    }
    else{
        
        [requestDic setValue:complete forKey:[NSString stringWithFormat:@"%lu",(long)[complete hash]]];
        [self startLocation];
    }
    
}


@end
